//
//  NSString+XDNSDate.m
//  tangchengyipin
//
//  Created by XiangDongMBP on 16/3/18.
//  Copyright © 2016年 XiangDong. All rights reserved.
//

#import "NSString+XDNSDate.h"
#import "NSDate+XDTimerFormatter.h"

@implementation NSString (XDNSDate)

- (NSDate *)xd_yyyyMMddStrToDate {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    return [formatter dateFromString:self];
}
- (NSDate *)xd_yyyyMMddHHmmssStrToDate {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    return [formatter dateFromString:self];
}
@end


@implementation NSString (XDNSDateNSString)
- (NSString *)xd_yyyyMMddHHmmssStrToFriendDateStr {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [formatter dateFromString:self];
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    NSString *str = nil;
    if (interval < 60) {
        str = @"刚刚";
    } else if (interval < 3600) {
        str = [NSString stringWithFormat:@"%ld分钟前", (long)(interval / 60.0)];
    } else if (interval < 24*3600) {
        str = [NSString stringWithFormat:@"%ld小时前", (long)(interval / 3600.0)];
    } else {
        str = [date xd_MMddHHmm];
    }
    return  str;
}

@end
